package com.qcdxs.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcdxs.entity.TUser;
import com.qcdxs.mapper.TUserMapper;
import com.qcdxs.service.TUserService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.logging.log4j.LogManager;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author panting
 * @since 2019-09-25
 */
//@RestController 注解： 该注解是 @Controller 和 @ResponseBody 注解的合体版
@RestController
@RequestMapping("/tuser")
public class TUserController {

  @Autowired
  private TUserService tUserService;
  @Resource
  private TUserMapper tUserMapper;

  private static org.slf4j.Logger log = LoggerFactory.getLogger(LogManager.class);

  @RequestMapping("/test")
  //@PostMapping("/test")
  @ResponseBody
  public Object test1() {

    // 插入新记录
    TUser tUser = new TUser();
    tUser.setTelephone("1323715"+(int)(Math.random()*1000));
    tUser.setPassword("111");
    tUser.setUsername("22");
    tUser.setRemark("2222");
    tUserService.save(tUser);

    // 或者
    tUser.insertOrUpdate();
    // 更新完成后，mpUser对象的id会被补全
    log.info("tUser={}", tUser.toString());

    // 通过主键id查询
    tUser = tUserService.getById(tUser.getId());
    log.info("mpUser={}", tUser.toString());
    // 条件查询，下面相当于xml中的 select * from mp_user where address = '"广东深圳' and username = 'David Hong' limit 1
//    mpUser = mpUserService.getOne(
//        new QueryWrapper<MpUser>().eq("address", "广东深圳").eq("username", "David Hong")
//            .last("limit 1"));
    // 批量查询
    List<TUser> mpUserList = tUserService.list();
    // 分页查询
    int pageNum = 1;
    int pageSize = 2;

    Page<TUser> page = new Page<>(pageNum, pageSize);
    //IPage<TUser> list= tUserMapper.selectPageVo(page);
    //IPage<TUser> all = tUserMapper.selectPage(new Page<>(pageNum, pageSize));

    IPage<TUser> mpUserIPage1 = tUserService
        .page(new Page<>(pageNum, pageSize), new QueryWrapper<TUser>().eq("username", "潘婷21"));
    IPage<TUser> mpUserIPage = tUserService
        .page(new Page<>(pageNum, pageSize), new QueryWrapper<TUser>());
    log.info(mpUserIPage.toString());
    // IPage to List
    List<TUser> mpUserList1 = mpUserIPage.getRecords();
    //log.info(mpUserList1.toString());
    // 总页数
    long allPageNum = mpUserIPage.getPages();
    log.info(allPageNum+"");
    // 修改更新
    tUser.setRemark("广东广州");
    tUserService.updateById(tUser);
    // 或者
    //tUser.insertOrUpdate();

    // 通过主键id删除
    //tUserService.removeById(1);
    // 或者
    //tUser.deleteById();
    Map map=new HashMap();
    map.put("tUser",tUser);
    //return "1111";
    return map;
  }
}
